package de.fh.ffm.elevator;

/**
 * abstract Door base class
 * 
 * @author harscher <swe.chr@googlemail.com>
 * 
 */
public abstract class Door {

	/**
	 * doors can be open or closed
	 * 
	 * is closed by default
	 */
	private boolean open = false;

	/**
	 * close the door
	 */
	public void close () {
		open = false;
	}

	/**
	 * open the door, uses abstract canBeOpened method to check
	 * 
	 * @see canBeOpened
	 * 
	 * @throws CannotBeOpenedException
	 *             if the doors can't be opened
	 */
	public void open () {
		if ( !canBeOpened() ) {
			throw new CannotBeOpenedException();
		}

		open = true;
	}

	/**
	 * checks if this Door can be opened
	 * 
	 * @return true if it can be opened; false otherwise
	 */
	public abstract boolean canBeOpened ();

	/**
	 * check if the doors are open
	 * 
	 * @return true if the doors are open; false otherwise;
	 */
	public boolean isOpen () {
		return open;
	}

	/**
	 * Is thrown when it is tried to open a Door when the conditions don't allow
	 * it
	 * 
	 * @author mann <mann.mario@googlemail.com>
	 */
	public static class CannotBeOpenedException extends RuntimeException {
		private static final long serialVersionUID = 1L;
	}

	/**
	 * Is thrown when someone tries to go through a closed Door
	 * 
	 * @author mann <mann.mario@googlemail.com>
	 */
	public static class DoorClosedException extends RuntimeException {
		private static final long serialVersionUID = 1L;
	}

}
